﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using CNShapes;

namespace CNPageViewer
{
    /// <summary>
    /// Contains static helper functions that are useful when rendering.
    /// </summary>
    public static class RendererHelper
    {
        #region Public static functions

        /// <summary>
        /// Returns the pen to use to draw this shape, using data from a shape
        /// derived from ShapeBase_DrawnWithPen. The width of the pen is scaled by
        /// the shape-viewer-controls zoom factor.
        /// </summary>
        public static Pen getPen(ShapeBase_DrawnWithPen shape, PageViewerControl pageViewerControl)
        {
            Pen pen = new Pen(shape.getPenColor());
            pen.Width = shape.getPenWidth() * pageViewerControl.ZoomFactor;
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
            return pen;
        }

        /// <summary>
        /// Returns the bounds of a shape derived from ShapeBase_RectangularBounds.
        /// The bounds translated into the client coordinates of the PageViewerControl 
        /// passed in.
        /// </summary>
        public static RectangleF getBoundsInClientCoordinates(ShapeBase_RectangularBounds shape, PageViewerControl pageViewerControl)
        {
            // We find the top-left and bottom-right points of the bounds...
            Graphics graphics = pageViewerControl.CreateGraphics();
            RectangleF bounds = shape.getBounds(graphics);
            PointF topLeft = new PointF(bounds.Left, bounds.Top);
            PointF bottomRight = new PointF(bounds.Right, bounds.Bottom);

            // We translate these into client coordinates for the viewer-control...
            PointF clientTopLeft = pageViewerControl.shapePointToClientPoint(topLeft);
            PointF clientBottomRight = pageViewerControl.shapePointToClientPoint(bottomRight);

            RectangleF results = new RectangleF(clientTopLeft.X, clientTopLeft.Y, clientBottomRight.X - clientTopLeft.X, clientBottomRight.Y - clientTopLeft.Y);
            return results;
        }

        /// <summary>
        /// Draws a light gray box around the bounds of the shape passed in.
        /// </summary>
        public static void showBoundsBox(Shape shape, Graphics graphics, PageViewerControl pageViewerControl)
        {
            RectangleF bounds = shape.getBounds(graphics);

            PointF shapeTopLeft = new PointF(bounds.X, bounds.Y);
            PointF screenTopLeft = pageViewerControl.shapePointToClientPoint(shapeTopLeft);

            PointF shapeBottomRight = new PointF(bounds.Right, bounds.Bottom);
            PointF screenBottomRight = pageViewerControl.shapePointToClientPoint(shapeBottomRight);

            float fWidth = screenBottomRight.X - screenTopLeft.X;
            float fHeight = screenBottomRight.Y - screenTopLeft.Y;

            Pen pen = new Pen(Color.LightGray);
            graphics.DrawRectangle(pen, screenTopLeft.X, screenTopLeft.Y, fWidth, fHeight);
        }

        #endregion
    }
}
